<?php

	if(!isset($_SESSION)) { session_start(); }

?>

<div class="user_vue">

	<h2> Profil utilisateur </h2>
	<div id="resultatChangement"> </div>
	<table>
		<tr>
			<form id="changeNom" action="#">
			<td><label> Nom : </label></td><td><input id="newUsername" name="newUsername" value="" type="text" autocomplete="on" placeholder=<?php echo $_SESSION['username'] ?> required></td><td><button class="boutonUser" type="submit">Enregistrer</button></td>
			</form>
		</tr>
		<tr>
			<form id="changeMail" action="#">
			<td><label> E-Mail : </label></td><td><input id="newMail" name="newMail" value="" type="text" autocomplete="on" placeholder=<?php echo $_SESSION['mail'] ?> required></td><td><button class="boutonUser" type="submit">Enregistrer</button></td>
			</form>
		</tr>
		<tr>
			<td> Statut : </td><td><?php if($_SESSION['statut'] == 3) { echo "Membre"; } else { echo "Administrateur"; } ?></td><td></td>
		</tr>
	</table>	
	<br/><br/>
	<table>
	<form id="changeMdp" action="#">
		<h2>Modifier le mot de passe</h2>
		<div id="resultatChangementMdp"> </div>
		<table>
			<tr>
				<td><label class="oldMdp">Ancien mot de passe</label></td>
				<td><input id="oldMdp" name="oldMdp" value="" type="password" autocomplete="on" placeholder="Ancien mot de passe" required></td>
			</tr>
			<tr>
				<td><label class="newMdp">Nouveau mot de passe</label></td>
				<td><input id="newMdp" name="newMdp" value="" type="password" autocomplete="on" placeholder="Nouveau mot de passe" required></td>
			</tr>
			<tr>
				<td><label class="newConfMdp">Confirmation du nouveau mot de passe</label></td>
				<td><input id="newConfMdp" name="newConfMdp" value="" type="password" autocomplete="on" placeholder="Confirmation nouveau mot de passe" required></td>
			</tr>
		</table>
		<br/>
		<button class="boutonUser" type="submit" >Modifier</button>
	</form>

</div>

<script>

	$('#changeNom').submit(function() {

			var rep = true;
			$.ajaxSetup({async: false});
			$.post("src/Vue/verif.php",
			{
				newUsername : $("#newUsername").val()
			},

			function(data){
				if(data.substring(data.length -1) == "N") {
					$("#resultatChangement").html("Le nom existe déjà");
					rep = false;					
				}

			});
			$.ajaxSetup({async: true});
			return rep;
	});

	$('#changeMail').submit(function() {

			var rep = true;
			$.ajaxSetup({async: false});
			$.post("src/Vue/verif.php",
			{
				newMail : $("#newMail").val()
			},

			function(data){
				if(data.substring(data.length -1) == "N") {
					rep = false;
					$("#resultatChangement").html("Le mail existe déjà");
				}

			});
			$.ajaxSetup({async: true});
			return rep;
	});

	$('#changeMdp').submit(function() {

			var rep = true;

			if($("#newMdp").val() != $("#newConfMdp").val()) {
				$("#resultatChangementMdp").html("Mot de passe différent de la confirmation de mot de passe");
				rep = false;
			} else {
			
				$.ajaxSetup({async: false});
				$.post("src/Vue/verif.php",
				{
					oldMdp : $("#oldMdp").val(),
					newMdp : $("#newMdp").val()
				},

				function(data){					
					if(data.substring(data.length -1) == "N") {
						rep = false;
						$("#resultatChangementMdp").html("Mauvais ancien mot de passe");
					}

				});
				$.ajaxSetup({async: true});
			}

			return rep;
	});

</script>